
class TestGeneric{
  void start(){
    print("======== 一、类继承 =========");
    Student stu1 = Student.withContrutor();
    stu1.className = "一班";
    stu1.name = "程自如";
    stu1.age = 1;
    print("stu1 = $stu1");

    print("======== 二、泛型类 =========");
    Cache<String> cache1 = Cache();
    cache1.setItem("cache1", "cache1");
    String val1 = cache1.getItem("cache1");
    print("val1 = $val1");

    Cache<int> cache2 = Cache();
    cache2.setItem("cache2", 1080);
    int val2 = cache2.getItem("cache2");
    print("val2 = $val2");

    print("======== 三、约束的泛型类 =========");
    Member<Student> member = Member(Student("小班", "张小", 2));
    print("${member.fixedName()}");
  }
}

/**
 *  泛型类
 *  提高代码复用
 */
class Cache<T>{

  static final Map<String, Object> _cache = Map();

  void setItem(String key, T val){
    _cache[key] = val;
  }

  T getItem(String key){
    return _cache[key];
  }
}

/**
 * 有时候你在实现类似通用接口的泛型中，期望的类型是某些特定类型时，这时可以使用类型约束
 */
class Member<T extends Person>{
  T _person;
  Member(this._person);

  String fixedName(){
    return 'fixed:${_person.name}';
  }
}

class Student extends Person{
  String className;

  Student(this.className, name, age) : super(name, age);

  Student.withContrutor() : super.withContrutor();

  @override
  String toString() {
    return "name:$name, age:$age, className:$className, money:$money";
  }
}


class Person{
  String name;
  int age;

  Person(this.name, this.age);

  Person.withContrutor();

  set money(val){
    this.age = val+1;
  }

  int get money => 1000 * age;
}